<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxSumAfterPartitioning = function (arr, k) {
      const n = arr.length;
      if (n <= k) {
        return Math.max(...arr) * k;
      }
      const dp = Array(n + 1).fill(0);
      for (let i = 1; i <= n; i++) {
        for (let j = i - 1, max = 0; j >= Math.max(0, i - k); j--) {
          max = Math.max(max, arr[j]);
          dp[i] = Math.max(dp[i], dp[j] + max * (i - j));
        }
      }
      return dp[n];
    };
    console.log(maxSumAfterPartitioning([1, 15, 7, 9, 2, 5, 10], 3))
  </script>
</body>

</html>